﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuanLiSinhVien_v01.DTO;

namespace QuanLiSinhVien_v01.DAO
{
    class KhoaHocDao: DataProvider
    {
        public KhoaHocDto KhoaHoc { get; set; }

        protected override Object GetDataFromDataRow(DataTable dt, int rowIndex)
        {
            KhoaHocDto KhoaHocDto = new KhoaHocDto();
            KhoaHocDto.MaKhoaHoc = dt.Rows[rowIndex]["MaKhoaHoc"].ToString();
            KhoaHocDto.TenKhoaHoc = dt.Rows[rowIndex]["TenKhoaHoc"].ToString();
            KhoaHocDto.NamBatDau = Int32.Parse(dt.Rows[rowIndex]["NamBatDau"].ToString());
            KhoaHocDto.NamKetThuc = Int32.Parse(dt.Rows[rowIndex]["NamKetThuc"].ToString());
            KhoaHocDto.GhiChu = dt.Rows[rowIndex]["GhiChu"].ToString();

            return KhoaHocDto;
        }

        public KhoaHocDto[] GetList()
        {
            string strCommand = " Select * from KhoaHoc ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            KhoaHocDto[] kq = new KhoaHocDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (KhoaHocDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }

        public void Insert()
        {
            string strCommand =
                "insert into KhoaHoc (MaKhoaHoc, TenKhoaHoc, NamBatDau, NamKetThuc , GhiChu) " +
                "values (N'" + KhoaHoc.MaKhoaHoc + "', N'" +
                              KhoaHoc.TenKhoaHoc + "', " +
                              KhoaHoc.NamBatDau + ", " +
                              KhoaHoc.NamKetThuc + ", N'" +
                              KhoaHoc.GhiChu + "'" +
                        ")";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update()
        {
            string strCommand =
                "update KhoaHoc set " +
                "TenKhoaHoc = N'" + KhoaHoc.TenKhoaHoc + "' , " +
                "NamBatDau = " + KhoaHoc.NamBatDau + " , " +
                "NamKetThuc = " + KhoaHoc.NamKetThuc + " , " +
                "GhiChu = N'" + KhoaHoc.GhiChu + "' " +
                "where MaKhoaHoc = N'" + KhoaHoc.MaKhoaHoc + "' ";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete()
        {
            string strCommand =
                "DELETE FROM KhoaHoc " +
                "WHERE  MaKhoaHoc = '" + KhoaHoc.MaKhoaHoc + "'";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
